home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / popup / popup.cpp.z / popup.cpp
Encoding:
C/C++ Source or Header  |  2002-04-08  |  4.1 KB  |  134 lines

  1. /****************************************************************************
  2. ** $Id:  qt/popup.cpp   3.0.3   edited Oct 12 12:18 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "popup.h"
  12. #include <qapplication.h>
  13. #include <qlayout.h>
  14.  
  15. FancyPopup::FancyPopup( QWidget* parent, const char*  name ):
  16.     QLabel( parent, name, WType_Popup ){
  17.         setFrameStyle( WinPanel|Raised );
  18.         setAlignment( AlignCenter );
  19.         resize(150,100);
  20.         moves = 0;
  21.         setMouseTracking( TRUE );
  22. }
  23.  
  24. void FancyPopup::mouseMoveEvent( QMouseEvent * e){
  25.     moves++;
  26.     QString s;
  27.     s.sprintf("%d/%d", e->pos().x(), e->pos().y());
  28.     if (e->state() & QMouseEvent::LeftButton)
  29.         s += " (down)";
  30.     setText(s);
  31. }
  32.  
  33. void FancyPopup::mouseReleaseEvent( QMouseEvent * e){
  34.     if  (rect().contains( e->pos() ) || moves > 5)
  35.         close();
  36. }
  37.  
  38. void FancyPopup::closeEvent( QCloseEvent *e ){
  39.     e->accept();
  40.     moves = 0;
  41.     if (!popupParent)
  42.         return;
  43.  
  44.     // remember that we (as a popup) might recieve the mouse release
  45.     // event instead of the popupParent. This is due to the fact that
  46.     // the popupParent popped us up in its mousePressEvent handler. To
  47.     // avoid the button remaining in pressed state we simply send a
  48.     // faked mouse button release event to it.
  49.     QMouseEvent me( QEvent::MouseButtonRelease, QPoint(0,0), QPoint(0,0), QMouseEvent::LeftButton, QMouseEvent::NoButton);
  50.     QApplication::sendEvent( popupParent, &me );
  51. }
  52.  
  53. void FancyPopup::popup( QWidget* parent) {
  54.     popupParent = parent;
  55.     setText("Move the mouse!");
  56.     if (popupParent)
  57.         move( popupParent->mapToGlobal( popupParent->rect().bottomLeft() ) );
  58.     show();
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. Frame::Frame(QWidget* parent, const char* name): QFrame(parent, name){
  67.     button1 = new QPushButton("Simple Popup", this);
  68.     connect ( button1, SIGNAL( clicked() ), SLOT( button1Clicked() ) );
  69.     button2 = new QPushButton("Fancy Popup", this);
  70.     connect ( button2, SIGNAL( pressed() ), SLOT( button2Pressed() ) );
  71.  
  72.     QBoxLayout * l = new QHBoxLayout( this );
  73.     button1->setMaximumSize(button1->sizeHint());
  74.     button2->setMaximumSize(button2->sizeHint());
  75.     l->addWidget( button1 );
  76.     l->addWidget( button2 );
  77.     l->activate();
  78.  
  79. //     button1->setGeometry(20,20,100,30);
  80. //     button2->setGeometry(140,20,100,30);
  81.     resize(270, 70);
  82.  
  83.     //create a very simple popup: it is just composed with other
  84.     //widget and will be shown after clicking on button1
  85.  
  86.     popup1 = new QFrame( this ,0, WType_Popup);
  87.     popup1->setFrameStyle( WinPanel|Raised );
  88.     popup1->resize(150,100);
  89.     QLineEdit *tmpE = new QLineEdit( popup1 );
  90.     connect( tmpE, SIGNAL( returnPressed() ), popup1, SLOT( hide() ) );
  91.     tmpE->setGeometry(10,10, 130, 30);
  92.     tmpE->setFocus();
  93.     QPushButton *tmpB = new QPushButton("Click me!", popup1);
  94.     connect( tmpB, SIGNAL( clicked() ), popup1, SLOT( close() ) );
  95.     tmpB->setGeometry(10, 50, 130, 30);
  96.  
  97.     // the fancier version uses its own class. It will be shown when
  98.     // pressing button2, so they behavior is more like a modern menu
  99.     // or toolbar.
  100.  
  101.     popup2 = new FancyPopup( this );
  102.  
  103.     // you might also add new widgets to the popup, just like you do
  104.     // it with any other widget.  The next four lines (if not
  105.     // commented out) will for instance add a line edit widget.
  106.  
  107. //     tmpE = new QLineEdit( popup2 );
  108. //     tmpE->setFocus();
  109. //     connect( tmpE, SIGNAL( returnPressed() ), popup2, SLOT( close() ) );
  110. //     tmpE->setGeometry(10, 10, 130, 30);
  111. }
  112.  
  113.  
  114. void Frame::button1Clicked(){
  115.     popup1->move( mapToGlobal( button1->geometry().bottomLeft() ) );
  116.     popup1->show();
  117. }
  118.  
  119. void Frame::button2Pressed(){
  120.     popup2->popup(button2);
  121. }
  122.  
  123.  
  124. int main( int argc, char **argv )
  125. {
  126.     QApplication a(argc,argv);
  127.  
  128.     Frame frame;
  129.     frame.setCaption("Qt Example - Custom Popups");
  130.     a.setMainWidget(&frame);
  131.     frame.show();
  132.     return a.exec();
  133. }
  134.